﻿<%@ WebHandler Language="C#" Class="ShowInfo" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Chenxin.Lib.Utility;

public class ShowInfo : AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：重写的请求处理方法
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：父类中隐藏了IHttpHandler接口方法，本类中须对本方法进行重写
    //作成者         ：姚志强
    //作成日         ：2010-03-04 
    //修改者         ：王玮
    //修改日         ：2010-03-25
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        
        //处理类型
        string strDealType = context.Request.Params["DealType"];

        switch (strDealType)
        {
            case "AddCompareHouse":
                AddCompareHouse(context);
                break;
            case "RemoveCompareHouse":
                RemoveCompareHouse(context);
                break;
            case "AddFavorsInfo":
                AddFavorsInfo(context);
                break;
            case "ViewOwnerInfo":
                ViewOwnerInfo(context);
                break;
            case "UpdateHouseInfo":
                UpdateHouseInfo(context);
                break;
            case "AddTaskDispenseInfo":
                AddTaskDispenseInfo(context);
                break;
        }
    }

    //*******************************************
    //功能概要       ：添加房源对比
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：添加房源对比
    //作成者         ：姚志强
    //作成日         ：2010-03-03
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void AddCompareHouse(HttpContext context)
    {
        string resultInfo = "";
        Dictionary<string, string> dicCompareHouseId;
        string houseId = context.Request.Params["houseId"];

        //从Cache中获取要对比的房源ID
        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);

        string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE, "comparehouseid");
        dicCompareHouseId = cs.GetObject(strCacheKey) as Dictionary<string, string>;

        if (!cs.Hit)
        {
            //如果Cache中没有信息,将当前的房源ID添加到Cache中
            dicCompareHouseId = new Dictionary<string, string>();
            dicCompareHouseId.Add(houseId, houseId);

            cs.Set(strCacheKey, dicCompareHouseId, DateTime.Today.AddDays(1));

            resultInfo = "添加成功";

        }
        else
        {
            if (dicCompareHouseId.ContainsKey(houseId))
            {
                resultInfo = "已添加到对比房源中";
            }
            else
            {
                dicCompareHouseId.Add(houseId, houseId);

                cs.Set(strCacheKey, dicCompareHouseId, DateTime.Today.AddDays(1));

                resultInfo = "添加成功";
            }
        }

        context.Response.Write(resultInfo);
    }

    //*******************************************
    //功能概要       ：移除房源对比
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：移除房源对比
    //作成者         ：姚志强
    //作成日         ：2010-03-03
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void RemoveCompareHouse(HttpContext context)
    {
        string resultInfo = "";
        Dictionary<string, string> dicCompareHouseId;
        string houseId = context.Request.Params["houseId"];

        //从Cache中获取要对比的房源ID
        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);

        string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE, "comparehouseid");
        dicCompareHouseId = cs.GetObject(strCacheKey) as Dictionary<string, string>;

        if (cs.Hit)
        {
            //如果存在要对比房源信息,将其从Cache中移除
            if (dicCompareHouseId.ContainsKey(houseId))
            {
                dicCompareHouseId.Remove(houseId);

                //更新对比房源的Cache                                                               
                cs.Set(strCacheKey, dicCompareHouseId, DateTime.Today.AddDays(1));

                //已将该房源从对比房源中移除
                resultInfo = "已将该房源从对比房源中移除";
            }
            else
            {
                resultInfo = "要移除的对比房源不存在";
            }
        }

        context.Response.Write(resultInfo);
    }

    //*******************************************
    //功能概要       ：收藏房源
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：收藏房源
    //作成者         ：姚志强
    //作成日         ：2010-03-03
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void AddFavorsInfo(HttpContext context)
    {
        string resultInfo = "";
        string houseId = context.Request.Params["houseId"];
        string houseAddress = context.Request.Params["houseAddress"];
        string favorType = context.Request.Params["favorType"];

        try
        {
            if (Chenxin.Lib.House.BLL.House_BaseInfo.ValidateIsExistFavors(houseId, CurrentUserInfo.UserId.ToString()))
            {
                resultInfo = "{" + string.Format("result:'true',msg:'{0}'", "已添加到收藏中") + "}";
            }
            else
            {
                //创建收藏实体对象
                Chenxin.Lib.House.Entity.Personal_FavorsInfo favorsInfo = new Chenxin.Lib.House.Entity.Personal_FavorsInfo();

                favorsInfo.RecordId = Guid.NewGuid();
                favorsInfo.FavorId = new Guid(houseId);
                if (!string.IsNullOrEmpty(favorType))
                {
                    favorsInfo.FavorType = Convert.ToInt16(favorType);
                }
                else
                {
                    favorsInfo.FavorType = 0;
                }
                
                favorsInfo.FavorName = houseAddress;
                favorsInfo.FavorPersonId = CurrentUserInfo.UserId;
                favorsInfo.FavorPerson = CurrentUserInfo.UserName;
                favorsInfo.AddTime = DateTime.Now;

                //添加收藏
                resultInfo = Chenxin.Lib.House.BLL.House_BaseInfo.AddFavorsInfo(favorsInfo);
            }

        }
        catch (Exception ex)
        {
            resultInfo = "{" + string.Format("result:'true',msg:'{0}'", ex.Message) + "}";
        }

        context.Response.Write(resultInfo);
    }

    //*******************************************
    //功能概要       ：添加查看信息的人
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：添加查看信息的人
    //作成者         ：姚志强
    //作成日         ：2010-03-03
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void ViewOwnerInfo(HttpContext context)
    {
        string resultInfo = "";
        string houseId = context.Request.Params["houseId"];
        
        try
        {
            Chenxin.Lib.House.Entity.House_ViewOwerInfo viewOwnerInfo = new Chenxin.Lib.House.Entity.House_ViewOwerInfo();
            Chenxin.Lib.House.Entity.House_BaseInfo houseinfo = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(new Guid(houseId));

            viewOwnerInfo.RecordId = Guid.NewGuid();
            viewOwnerInfo.HouseId = new Guid(houseId);
            viewOwnerInfo.HouseCode = houseinfo.HouseCode;
            viewOwnerInfo.HouseAddress = houseinfo.Community + houseinfo.BuildingName + houseinfo.Room;
            viewOwnerInfo.ViewEmployeeId = CurrentUserInfo.UserId;
            viewOwnerInfo.Employee = CurrentUserInfo.UserName;
            viewOwnerInfo.ViewDepartId = CurrentUserInfo.DepartId;
            viewOwnerInfo.Depart = CurrentUserInfo.DepartName;
            viewOwnerInfo.Addtime = DateTime.Now;

            resultInfo = Chenxin.Lib.House.BLL.House_BaseInfo.AddViewOwnerInfo(viewOwnerInfo);

            //resultInfo = "添加成功";

        }
        catch (Exception ex)
        {
            resultInfo = "{" + string.Format("result:'true',msg:'{0}'", ex.Message) + "}";
        }

        context.Response.Write(resultInfo);
    }

    //*******************************************
    //功能概要       ：修改房源基本信息
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：修改房源基本信息
    //作成者         ：姚志强
    //作成日         ：2010-03-03
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void UpdateHouseInfo(HttpContext context)
    {
        string resultInfo = "";

        try
        {
            string data = context.Request.Params["data"];
                
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string, object> jsonHouseInfo = (Dictionary<string, object>)serializer.DeserializeObject(data);

            //声明房源基本信息实体对象
            Chenxin.Lib.House.Entity.House_BaseInfo houseBaseInfo = Chenxin.Lib.House.DAL.House_BaseInfo.GetModel(new Guid(Convert.ToString(jsonHouseInfo["_houseId"])));

            if (houseBaseInfo == null)
            {
                resultInfo = "{" + string.Format("result:'false',msg:'{0}'", "要更新的数据已被删除") + "}";
            }
            else
            {
                houseBaseInfo.HouseId = new Guid(Convert.ToString(jsonHouseInfo["_houseId"]));
                houseBaseInfo.Region = Convert.ToString(jsonHouseInfo["_region"]);
                houseBaseInfo.Block = Convert.ToString(jsonHouseInfo["_block"]);
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_businessKind"])))
                {
                    houseBaseInfo.BusinessKind = Convert.ToInt32(jsonHouseInfo["_businessKind"]);
                }
                else
                {
                    houseBaseInfo.BusinessKind = 0;
                }
                if (jsonHouseInfo.ContainsKey("_status"))
                {
                    houseBaseInfo.Status = Convert.ToInt32(jsonHouseInfo["_status"]);
                }
                //if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_status"])))
                //{
                //    houseBaseInfo.Status = Convert.ToInt32(jsonHouseInfo["_status"]);
                //}
                houseBaseInfo.HouseUse = Convert.ToString(jsonHouseInfo["_houseUse"]);
                houseBaseInfo.BuildingType = Convert.ToString(jsonHouseInfo["_buildingType"]);
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_salePrice_T"])))
                {
                    houseBaseInfo.SalePrice_T = Convert.ToDecimal(jsonHouseInfo["_salePrice_T"]) * 10000;
                }
                else
                {
                    houseBaseInfo.SalePrice_T = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_salePrice_R"])))
                {
                    houseBaseInfo.SalePrice_R = Convert.ToDecimal(jsonHouseInfo["_salePrice_R"]) * 10000;
                }
                else
                {
                    houseBaseInfo.SalePrice_R = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_rentPrice"])))
                {
                    houseBaseInfo.RentPrice = Convert.ToDecimal(jsonHouseInfo["_rentPrice"]);
                }
                else
                {
                    houseBaseInfo.RentPrice = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_rentPrice_R"])))
                {
                    houseBaseInfo.RentPrice_R = Convert.ToDecimal(jsonHouseInfo["_rentPrice_R"]);
                }
                else
                {
                    houseBaseInfo.RentPrice_R = 0;
                }
                houseBaseInfo.IsGroup = Convert.ToBoolean(jsonHouseInfo["_isGroup"]);
                houseBaseInfo.PayMode = Convert.ToString(jsonHouseInfo["_payMode"]);
                houseBaseInfo.Fitment = Convert.ToString(jsonHouseInfo["_fitment"]);
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_buildingArea"])))
                {
                    houseBaseInfo.BuildingArea = Convert.ToDecimal(jsonHouseInfo["_buildingArea"]);
                }
                else
                {
                    houseBaseInfo.BuildingArea = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_model_Room"])))
                {
                    houseBaseInfo.Model_Room = Convert.ToInt32(jsonHouseInfo["_model_Room"]);
                }
                else
                {
                    houseBaseInfo.Model_Room = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_model_Hall"])))
                {
                    houseBaseInfo.Model_Hall = Convert.ToInt32(jsonHouseInfo["_model_Hall"]);
                }
                else
                {
                    houseBaseInfo.Model_Hall = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_model_Toilet"])))
                {
                    houseBaseInfo.Model_Toilet = Convert.ToInt32(jsonHouseInfo["_model_Toilet"]);
                }
                else
                {
                    houseBaseInfo.Model_Toilet = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_model_Balcony"])))
                {
                    houseBaseInfo.Model_Balcony = Convert.ToInt32(jsonHouseInfo["_model_Balcony"]);
                }
                else
                {
                    houseBaseInfo.Model_Balcony = 0;
                }
                houseBaseInfo.Direction = Convert.ToString(jsonHouseInfo["_direction"]);
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_cur_Floor"])))
                {
                    houseBaseInfo.Cur_Floor = Convert.ToInt32(jsonHouseInfo["_cur_Floor"]);
                }
                else
                {
                    houseBaseInfo.Cur_Floor = 0;
                }
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_total_Floor"])))
                {
                    houseBaseInfo.Total_Floor = Convert.ToInt32(jsonHouseInfo["_total_Floor"]);
                }
                else
                {
                    houseBaseInfo.Total_Floor = 0;
                }
                houseBaseInfo.Actuality = Convert.ToString(jsonHouseInfo["_actuality"]);
                houseBaseInfo.Property = Convert.ToString(jsonHouseInfo["_property"]);
                if (!string.IsNullOrEmpty(Convert.ToString(jsonHouseInfo["_completeYear"])))
                {
                    houseBaseInfo.CompleteYear = Convert.ToInt32(jsonHouseInfo["_completeYear"]);
                }
                else
                {
                    houseBaseInfo.CompleteYear = 0;
                }
                houseBaseInfo.HavRentContract = Convert.ToBoolean(jsonHouseInfo["_havRentContract"]);
                houseBaseInfo.IsHavekey = Convert.ToBoolean(jsonHouseInfo["_isHavekey"]);
                houseBaseInfo.IsTrust = Convert.ToBoolean(jsonHouseInfo["_isTrust"]);
                houseBaseInfo.InfoSource = Convert.ToString(jsonHouseInfo["_infoSource"]);
                houseBaseInfo.BaseEquip = Convert.ToString(jsonHouseInfo["_baseEquip"]).Replace("'", "").Replace("\r\n", ";").Replace("\n", ";").Replace("\r", ";");
                houseBaseInfo.Remarks = Convert.ToString(jsonHouseInfo["_remarks"]).Replace("'", "").Replace("\r\n", ";").Replace("\n", ";").Replace("\r", ";");

                houseBaseInfo.UpdateTime = DateTime.Now;

                houseBaseInfo.OprPersonId = CurrentUserInfo.UserId;
                houseBaseInfo.OprPerson = CurrentUserInfo.UserName;
                houseBaseInfo.OprDepartId = CurrentUserInfo.DepartId;
                houseBaseInfo.OprDepart = CurrentUserInfo.DepartName;

                //修改房源信息
                resultInfo = Chenxin.Lib.House.BLL.House_BaseInfo.UpdateHouseInfo(houseBaseInfo);
            }
        }
        catch (Exception ex)
        {
            resultInfo = "{" + string.Format("result:'false',msg:'{0}'", ex.Message) + "}";
        }

        context.Response.Write(resultInfo);
    }

    
    //*******************************************
    //功能概要       ：添加任务信息
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：添加任务信息
    //作成者         ：王玮
    //作成日         ：2010-03-25
    //修改者         ：
    //修改日         ：
    //*******************************************
    private void AddTaskDispenseInfo(HttpContext context)
    {

        string data = context.Request.Params["data"];

        JavaScriptSerializer jssr = new JavaScriptSerializer();
        Dictionary<string, object> jsonTaskDispenseInfo = (Dictionary<string, object>)jssr.DeserializeObject(data);

        Chenxin.Lib.House.Entity.Tasks_BaseInfo clhet = new Chenxin.Lib.House.Entity.Tasks_BaseInfo();
        try
        {
            clhet.RecordId = Guid.NewGuid();
            clhet.relatedId = new Guid(jsonTaskDispenseInfo["_houseid"].ToString());
            clhet.relatedCaption = Convert.ToString(jsonTaskDispenseInfo["_address"]);
            clhet.relatedCode = Convert.ToString(jsonTaskDispenseInfo["_housecode"]);
            clhet.taskReceiverId = new Guid(jsonTaskDispenseInfo["_taskuserid"].ToString());
            clhet.taskReceiver = Convert.ToString(jsonTaskDispenseInfo["_taskuser"]);
            clhet.ReceiverDepartId = new Guid(jsonTaskDispenseInfo["_departmentid"].ToString());
            clhet.ReceiverDepart = Convert.ToString(jsonTaskDispenseInfo["_department"]);
            clhet.TaskType = Convert.ToString(jsonTaskDispenseInfo["_tasktype"]);
            clhet.TaskBeginDay = Convert.ToDateTime(jsonTaskDispenseInfo["_startaddtime"]);
            clhet.TaskEndDay = Convert.ToDateTime(jsonTaskDispenseInfo["_endaddTime"]);
            clhet.Status = 0;
            clhet.Title = Convert.ToString(jsonTaskDispenseInfo["_title"]);
            clhet.Description = Convert.ToString(jsonTaskDispenseInfo["_describe"]);
            clhet.Addtime = DateTime.Now;
            clhet.AddPersonId = new Guid(jsonTaskDispenseInfo["_userid"].ToString());
            clhet.AddPerson = Convert.ToString(jsonTaskDispenseInfo["_username"]);
            clhet.AddDepartId = new Guid(jsonTaskDispenseInfo["_departid"].ToString());
            clhet.AddDepart = Convert.ToString(jsonTaskDispenseInfo["_departname"]);
            clhet.AddCompanyId = new Guid(jsonTaskDispenseInfo["_taskcompanyid"].ToString());
            clhet.AddCompany = Convert.ToString(jsonTaskDispenseInfo["_taskcompanyname"]);
            clhet.CityId = new Guid(jsonTaskDispenseInfo["_taskcityid"].ToString());
            clhet.CityName = Convert.ToString(jsonTaskDispenseInfo["_taskcityname"]);

            context.Response.Write(retExecResult(Constants.ExecResult.Succ, Chenxin.Lib.House.BLL.Tasks_BaseInfo.Add(clhet), ""));

        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
 
        }

    }

}